package com.artifact.mit.mypoint.adapter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.artifact.mit.mypoint.R;
import com.artifact.mit.mypoint.service.async.ImageLoader;
import com.artifact.mit.mypoint.service.model.SetImage;
import com.artifact.mit.mypoint.service.model.api.ProductItem;

public class ProductListAdapter extends ArrayAdapter<ProductItem.ProductItemDetail> {

	public ProductListAdapter(Context context) {
		super(context, 0);
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(getContext()).inflate(
					R.layout.catalogue_item, null);
		}
		ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
		Pattern pattern = Pattern.compile("(?<=src=\")(.*?)(?=\")");
		Matcher matcher = pattern.matcher(getItem(position).getUrlImage());
		String result = "";
		while (matcher.find()) {
			result = matcher.group(0);
		}
		new ImageLoader().execute(new SetImage(imageView, result, false));
		TextView title = (TextView) convertView.findViewById(R.id.textView1);
		TextView desc = (TextView) convertView.findViewById(R.id.textView2);
		TextView point = (TextView) convertView.findViewById(R.id.textView3);

//		imageView.setImageResource(getItem(position).img);
		title.setText(getItem(position).getName());
		point.setText("Point :"+getItem(position).getPoint());
//		desc.setText(getItem(position).desc);
		return convertView;
	}
}
